package com.qiwx.pigeon.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.data.params.CageAdjustParam;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description
 * @Author qiwx
 * @Date 2020-06-30 15:26
 * 鸽崽调窝
 **/
public class CageAdjustOptionView extends LinearLayout {
    @BindView(R.id.rb_number0)
    RadioButton rbNumber0;
    @BindView(R.id.rb_number1)
    RadioButton rbNumber1;
    @BindView(R.id.rb_number2)
    RadioButton rbNumber2;
    @BindView(R.id.rb_number3)
    RadioButton rbNumber3;
    @BindView(R.id.rb_number4)
    RadioButton rbNumber4;
    @BindView(R.id.rg_number)
    RadioGroup rgNumber;
    @BindView(R.id.cb_empty)
    CheckBox cbEmpty;

    public CageAdjustOptionView(Context context) {
        this(context, null);
    }

    public CageAdjustOptionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CageAdjustOptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initEvent();
    }

    private void initView(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.cage_adjust_option, null);
        ButterKnife.bind(this, view);
        addView(view);
        rbNumber4.setVisibility(GONE);
        rbNumber0.setVisibility(GONE);
        rbNumber1.setVisibility(GONE);
        rbNumber2.setVisibility(GONE);
        rbNumber3.setVisibility(GONE);
        resetOptions();


    }

    private void initEvent() {
        cbEmpty.setOnCheckedChangeListener((btn, isChecked) -> {
            if (isChecked) {
                rgNumber.setVisibility(GONE);
            } else {
                rgNumber.setVisibility(VISIBLE);
            }

        });


    }

    public CageAdjustParam getParam() {

        CageAdjustParam param = new CageAdjustParam();
        int amount = 0;//调整后的蛋数
        if (rbNumber0.isChecked()) {
            amount = 0;
        } else if (rbNumber1.isChecked()) {
            amount = 1;
        } else if (rbNumber2.isChecked()) {
            amount = 2;
        } else if (rbNumber3.isChecked()) {
            amount = 3;
        } else if (rbNumber4.isChecked()) {
            amount = 4;
        }
        param.setAmount(amount);
        param.setIs_empty(cbEmpty.isChecked() ? 1 : 0);
        return param;

    }


    //重置选项
    public void resetOptions() {
        rbNumber3.setChecked(true);
        cbEmpty.setChecked(false);

    }
}